﻿/*----------------------------------------------------------------
//  <copyright file="BatchSendParamsDto.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2023-09-14 11:03</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud.SMS.Dtos
{
    #region "短信批量发送参数DTO"
    /// <summary>
    /// 短信批量发送参数DTO
    /// </summary>
    public class BatchSendParamsDto
    {
        #region "属性"
        #region "获取或设置 短信类型"
        /// <summary>
        /// 获取或设置 短信类型
        /// </summary>
        public SmsType SmsType { get; set; }
        #endregion

        #region "获取或设置 短信批量发送参数DTO行信息"
        /// <summary>
        /// 获取或设置 短信批量发送参数DTO行信息
        /// </summary>
        public List<BatchSendParamsDtoLine> Lines { get; } = new List<BatchSendParamsDtoLine>();
        #endregion

        #endregion

        #region "方法"
        #region "获取行参数的JSON数组格式"
        /// <summary>
        /// 获取行参数的JSON数组格式
        /// </summary>
        /// <param name="PhoneNumberJson">接收短信的手机号码的JSON数组格式</param>
        /// <param name="SignNameJson">短信签名名称的JSON数组格式</param>
        /// <param name="TemplateParamJson">短信模板变量对应的实际值的JSON格式</param>
        /// <param name="UpExtendCodeJson">上行短信扩展码的JSON数组格式</param>
        public void GetLineParamsJsonString(out string PhoneNumberJson, out string SignNameJson
            , out string TemplateParamJson
            , out string UpExtendCodeJson)
        {
            //示例：["15900000000","13500000000"]
            PhoneNumberJson = "";

            //示例：["阿里云","阿里巴巴"]
            SignNameJson = "";

            //示例：[{"name":"TemplateParamJson1"},{"name":"TemplateParamJson2"}]
            TemplateParamJson = "";

            //示例：["90999","90998"]
            UpExtendCodeJson = "";

            VerifyLine();

            var lastItem = Lines.Last();
            foreach (var line in Lines)
            {
                if (line == lastItem)
                {
                    PhoneNumberJson += string.Format("\"{0}\"", line.PhoneNumber);
                    SignNameJson += string.Format("\"{0}\"", line.SignName);
                    TemplateParamJson = line.TemplateParam;
                    UpExtendCodeJson += string.Format("\"{0}\"", line.UpExtendCode);
                }
                else
                {
                    PhoneNumberJson += string.Format("\"{0}\"{1}", line.PhoneNumber, ",");
                    SignNameJson += string.Format("\"{0}\"{1}", line.SignName, ",");
                    TemplateParamJson = string.Format("{0}{1}", line.TemplateParam, ",");
                    UpExtendCodeJson += string.Format("\"{0}\"{1}", line.UpExtendCode, ",");
                }
            }
        }
        #endregion
        #region "获取行参数的合并字符串格式"
        /// <summary>
        /// 获取行参数的合并字符串格式
        /// </summary>
        /// <param name="PhoneNumberJoin">接收短信的手机号码的合并字符串格式</param>
        /// <param name="SignNameJoin">短信签名名称的合并字符串格式</param>
        /// <param name="TemplateParamJoin">短信模板变量对应的实际值的合并字符串格式</param>
        /// <param name="MsgContentJoin">短信内容的合并字符串格式</param>
        /// <param name="SmsUpExtendCodeJoin">上行短信扩展码的合并字符串格式</param>
        /// <param name="SplitStr">合并的分隔符，默认“,”</param>
        public void GetLineParamsJoinString(out string PhoneNumberJoin, out string SignNameJoin
            , out string TemplateParamJoin
            , out string MsgContentJoin
            , out string SmsUpExtendCodeJoin
            , string SplitStr = ",")
        {
            //示例：15900000000,13500000000
            PhoneNumberJoin = "";

            //示例：阿里云,阿里巴巴
            SignNameJoin = "";

            //示例：{"name":"TemplateParamJson1"},{"name":"TemplateParamJson2"}
            MsgContentJoin = "";

            //示例：【微云】短信1，【微云网】短信2
            TemplateParamJoin = "";

            //示例：90999,90998
            SmsUpExtendCodeJoin = "";

            VerifyLine();

            var lastItem = Lines.Last();
            foreach (var line in Lines)
            {
                if (line == lastItem)
                {
                    PhoneNumberJoin += string.Format("{0}", line.PhoneNumber);
                    SignNameJoin += string.Format("{0}", line.SignName);
                    TemplateParamJoin = line.TemplateParam;
                    MsgContentJoin += string.Format("{0}", line.MsgContent);
                    SmsUpExtendCodeJoin += string.Format("{0}", line.UpExtendCode);
                }
                else
                {
                    PhoneNumberJoin += string.Format("{0}{1}", line.PhoneNumber, SplitStr);
                    SignNameJoin += string.Format("{0}{1}", line.SignName, SplitStr);
                    TemplateParamJoin = string.Format("{0}{1}", line.TemplateParam, SplitStr);
                    MsgContentJoin = string.Format("{0}{1}", line.MsgContent, SplitStr);
                    SmsUpExtendCodeJoin += string.Format("{0}{1}", line.UpExtendCode, SplitStr);
                }
            }
        }
        #endregion

        #endregion

        #region "私有方法"
        #region "验证行信息"
        /// <summary>
        /// 验证行信息
        /// </summary>
        private void VerifyLine()
        {
            if (Lines == null)
            {
                throw new Exception(I18N.T("短信批量发送信息为空"));
            }
            if (Lines.Count < 1)
            {
                throw new Exception(I18N.T("短信批量发送信息为空"));
            }
            if (Lines.Exists(o => o.PhoneNumber.IsMissing()))
            {
                throw new Exception(I18N.T("存在为空的手机号记录"));
            }
        }
        #endregion

        #endregion

    }
    #endregion

    #region "短信批量发送参数DTO行信息"
    /// <summary>
    /// 短信批量发送参数DTO行信息
    /// </summary>
    public class BatchSendParamsDtoLine
    {
        #region "属性"
        #region "获取或设置 接收短信的手机号码"
        private string _PhoneNumber = "";
        /// <summary>
        /// 获取或设置 接收短信的手机号码
        /// </summary>
        public string PhoneNumber
        {
            get
            {
                if (_PhoneNumber.IsMissing())
                {
                    throw new Exception(I18N.T("接收短信的手机号码为空"));
                }
                return _PhoneNumber;
            }
            set { _PhoneNumber = value; }
        }
        #endregion
        #region "获取或设置 短信签名名称"
        private string _SignName = "";
        /// <summary>
        /// 获取或设置 短信签名名称
        /// </summary>
        public string SignName
        {
            get
            {
                if (_SignName.IsMissing())
                {
                    throw new Exception(I18N.T("短信签名名称为空"));
                }
                return _SignName;
            }
            set { _SignName = value; }
        }
        #endregion
        #region "获取或设置 模板中的变量Json字符串"
        /// <summary>
        /// 获取或设置 模板中的变量Json字符串
        /// </summary>
        public string TemplateParam { get; set; }
        #endregion

        #region "获取或设置 短信内容"
        /// <summary>
        /// 获取或设置 短信内容
        /// </summary>
        public string MsgContent { get; set; }
        #endregion

        #region "获取或设置 上行短信扩展码"
        /// <summary>
        /// 获取或设置 上行短信扩展码
        /// 无特殊需要此字段的用户请忽略此字段
        /// </summary>
        public string UpExtendCode { get; set; }
        #endregion

        #endregion

    }
    #endregion

}
